package com.bosssoft.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibUserRoleDTO;
import com.bosssoft.entity.po.FibRoleAuthPO;
import com.bosssoft.entity.po.FibUserPO;
import com.bosssoft.entity.po.FibUserRolePO;
import com.bosssoft.entity.vo.FibUserRoleVO;
import com.bosssoft.entity.vo.FibUserVO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 项昊天
 * @create 2021/8/17 20:20
 */
@Slf4j
@RestController
@RequestMapping("/userrole")
public class UserRoleController {

    @Resource
    FibUserRoleService userRoleService;

    @PostMapping("/save")
    public Result saveUserRole(@RequestBody @Validated FibUserRoleVO userRoleVO) {
        try {
            Byte deleted = 1;
            userRoleVO.setDeleted(deleted);
            FibUserRoleDTO userRoleDTO = new FibUserRoleDTO();
            BeanUtils.copyProperties(userRoleVO, userRoleDTO);
            if (userRoleService.addUserRole(userRoleDTO)) {
                return Result.ok().message("添加用户角色联系成功!");
            }
            return Result.error().message("添加用户角色联系失败!");
        } catch (Exception e) {
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/getUserRolePage")
    public Result getUserRolePage(@RequestParam(defaultValue = "1") Integer pageNo,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  FibUserRoleVO fibUserRoleVO) {
        try {
            Page<FibUserRolePO> page = new Page<>(pageNo, pageSize);
            QueryWrapper<FibUserRolePO> queryWrapper = new QueryWrapper<>();
            if (ObjectUtil.isNotNull(fibUserRoleVO.getUserId())) {
                queryWrapper.like("user_id", fibUserRoleVO.getUserId());
            }
            if (ObjectUtil.isNotNull(fibUserRoleVO.getRoleId())) {
                queryWrapper.like("role_id", fibUserRoleVO.getRoleId());
            }
            Page<FibUserRolePO> userRolePage = userRoleService.page(page);
            long total = userRolePage.getTotal();
            List<FibUserRolePO> recordPOs = userRolePage.getRecords();
            PageResult<FibUserRolePO> results = new PageResult<>(page, recordPOs);
            return Result.ok().data("total", total)
                    .data("page", results)
                    .data("pageNo", pageNo);
        } catch (Exception e) {
            log.info("getUserRolePage出现异常");
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PutMapping("/modify")
    public Result modifyUserRole(@RequestBody @Validated FibUserRoleVO userRoleVO) {
        try {
            FibUserRoleDTO userRoleDTO = new FibUserRoleDTO();
            BeanUtils.copyProperties(userRoleVO, userRoleDTO);
            if (userRoleService.modifyUserRole(userRoleDTO)) {
                return Result.ok().message("修改用户角色联系成功!");
            }
            return Result.error().message("修改用户角色联系失败!");
        } catch (Exception e) {
            log.info("modifyUserRole出问题啦!");
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PutMapping("/ban/{id}/{status}")
    public Result banUserRole(@PathVariable("id") Long id,
                              @PathVariable("status") Integer status) {
        try {
            if (userRoleService.banUserRoleById(id, status)) {
                return Result.ok().message("禁用用户角色关联成功!");
            }
            return Result.error().message("禁用用户角色关联失败!");
        } catch (Exception e) {
            log.info("banUserRole出错");
        }
        return Result.error().message("banUserRole出问题啦");
    }

    @DeleteMapping("/delete")
    public Result delUserRole(@RequestBody FibUserRoleVO userRoleVO) {
        try {
            if (userRoleVO.getId() != null && userRoleService.deleteUserRoleById(userRoleVO.getId())) {
                return Result.ok().message("删除用户角色关联成功!");
            }
            return Result.error().message("删除用户角色关联失败!");
        } catch (Exception e) {
            log.info("deleteUserRole错误");
        }
        return Result.error().message("deleteUserRole出问题啦");
    }

//  @DeleteMapping("/delete/{id}")
//  public Result delUserRole(@PathVariable("id") Long id) {
//    try {
//      if (userRoleService.deleteUserRoleById(id)) {
//        return Result.ok().message("删除用户角色关联成功!");
//      }
//      return Result.error().message("删除用户角色关联失败!");
//    } catch (Exception e) {
//      log.info("deleteUserRole错误");
//    }
//    return Result.error().message("deleteUserRole出问题啦");
//  }
}
